package org.ReportDesigner.reportModel;

import org.ReportDesigner.configurator.Configuration;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import org.ReportDesigner.controlModel.Container;
import org.ReportDesigner.controlModel.Text;
import org.ReportDesigner.controlModel.Type;
import org.apache.log4j.Logger;

import java.util.LinkedList;
import java.util.List;

@Type(type = "parent", subType = "report", name = "parent")
public abstract class ReportElement implements Container<ReportElement>, Text {
    private static final Logger log = Logger.getLogger(ReportElement.class);
    private List<ReportElement> body = new LinkedList<ReportElement>();
    private boolean closed = false;

    @Override
    public abstract String getText();

    @Override
    public abstract void setText(String text);

    @Override
    public List<ReportElement> getBody() {
        return this.body;
    }

    @Override
    public ReportElement getBodyElement(int i) {
        return body.get(i);
    }


    @Override
    public void addBodyElement(ReportElement element) {
        body.add(element);
    }

    public String getTypeName(){
        return this.getClass().getAnnotation(Type.class).name();
    }

    public String getType(){
        return this.getClass().getAnnotation(Type.class).type();
    }

    public String getSubType(){
        return this.getClass().getAnnotation(Type.class).subType();
    }


    public boolean isClosed() {
        return closed;
    }

    public void setClosed(boolean closed) {
        this.closed = closed;
    }

    public void addToDocument(Configuration configuration, Document document) throws DocumentException{
        //получаем представление тела элемента
        List<Element> elementList = addTo(configuration);
        //пошагово добавляем в тело документа представления
        for (Element element: elementList){
            document.add(element);
        }
    }


    public  List<Element> addTo(Configuration configuration){

            List<Element> elementList = new LinkedList<Element>();
            //получаем тело отчета
            for(ReportElement repElement: this.getBody()){
                //получаем представления дочерних элементов и добавляем в список
                elementList.addAll(repElement.addTo(configuration));
            }
            return elementList;
    }


}
